﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Dynamic;
using ChoViet.CMS.Properties;
using ChoViet.CMS.ViewModels;
using ChoViet.Model;
using ChoViet.Model.Properties;
using ChoViet.Lib.Common;
using ChoViet.Lib.Paging;
using ChoViet.Lib.Helpers;
namespace ChoViet.CMS.Controllers
{
	[RoleHelper(Roles = RoleSystem.Dictionary)]
    public class DistrictsController : BaseController
    {
        /// <summary>
        /// Created by Vietlv
        /// Date 17/06/2011
        /// Description: Manage Districts - Class Districts controler;
        /// </summary>
        /// <returns></returns>

        ChoVietDataContext eDataConttext = new ChoVietDataContext();

        #region GET: /Districts/
        public ActionResult IndexCms(int? page)
        {
            try
            {
                    if (page == null) page = 1;
                    string order = UserState.getUserStateFromRequest("Districts.orderColum", "order", "ID").ToString();
                    string orderDir = UserState.getUserStateFromRequest("Districts.orderDirection", "orderDir", "desc").ToString();
                    string keyword = UserState.getUserStateFromRequest("Districts.Keyword", "Keyword", "").ToString();
                    Int16 currentcity =Convert.ToInt16(UserState.getUserStateFromRequest("Districts.CityID", "CityID", 0).ToString());
                    int pagesize = Convert.ToInt32(UserState.getUserStateFromRequest("City.PageSize", "PageSize", Settings.Default.PageSize));
                    var listDistricts = eDataConttext.Districts
                        .Where(e => keyword.Trim().Length > 0 ? e.Name.ToLower().Contains(keyword.ToLower().Trim()) ||
                                     e.ShortName.ToLower().Contains(keyword.ToLower().Trim()) : true)
                        .Where(e => currentcity > 0 ? e.CityID == currentcity : true)
                        .OrderBy(string.Format("{0} {1}", order, orderDir))
                        .ToPagedList(page.Value - 1, pagesize);
                    var listcity = eDataConttext.Cities.ToList();
                    ViewData["order"] = order;
                    ViewData["orderDir"] = orderDir;
                    return View(new DistrictViewModel
                    {
                        Districts = listDistricts,
                        Cities = listcity,
                        Keyword = keyword,
                        currentCity = currentcity
                    });
            }
            catch (Exception exp)
            {
                ViewData["MessageInfo"] = exp.Message;
                Messages.AddErrorFlashMessage(exp.Message);
                return View();
            }
        }
        #endregion

        #region GET: /Districts/DetailsCms/5
        public ActionResult DetailsCms(Int16 id)
        {
                var itemDistricts = eDataConttext.Districts.Single(e => e.ID == id);
                return View(itemDistricts);
        }
        #endregion

        #region GET: /Districts/CreateCms
        public ActionResult CreateCms()
        {
                var listCity = eDataConttext.Cities.ToList();
                return View(new DistrictViewModel { 
               Cities = listCity 
                });
        }
        #endregion

        #region POST: /Districts/CreateCms

        [HttpPost]
        public ActionResult CreateCms(District fromdata)
        {
                try
                {
                    District item = new District();
                    item.CityID = fromdata.CityID;
                    item.Name = fromdata.Name;
                    item.ShortName = fromdata.ShortName;

                    eDataConttext.Districts.InsertOnSubmit(item);
                    eDataConttext.SubmitChanges();

                    return RedirectToAction("IndexCms");
                }
                catch (Exception exp)
                {
                    ViewData["MessageInfo"] = exp.Message;
                    Messages.AddErrorFlashMessage(exp.Message);
                    return View();
                }
        }
        #endregion

        #region GET: /Districts/DeleteCms/5
        public ActionResult DeleteCms(Int16 id)
        {
                try
                {
                    var item = eDataConttext.Districts.Single(e => e.ID == id);

                    return View(item);
                }
                catch (Exception exp)
                {
                    ViewData["MessageInfo"] = exp.Message;
                    Messages.AddErrorFlashMessage(exp.Message);
                    return View();
                }
        }
        #endregion

        #region POST: /Districts/DeleteCms/5

        [HttpPost]
        public ActionResult DeleteCms(Int16 id, FormCollection collection)
        {
                try
                {
                    var item = eDataConttext.Districts.Single(e => e.ID == id);
                    eDataConttext.Districts.DeleteOnSubmit(item);
                    eDataConttext.SubmitChanges();
                    return RedirectToAction("IndexCms");
                }
                catch (Exception exp)
                {
                    ViewData["MessageInfo"] = exp.Message;
                    Messages.AddErrorFlashMessage(exp.Message);
                    return View();
                }
        }
        #endregion

        #region GET: /City/EditCms/5
        public ActionResult EditCms(Int16 id)
        {
                try
                {
                    var item = eDataConttext.Districts.Single(e => e.ID == id);
                    var listCity = eDataConttext.Cities.ToList();
                    return View(new DistricEditViewModel
                    { 
                    Cities = listCity,
                    currentDistrict = item
                    });
                }
                catch (Exception exp)
                {
                    ViewData["MessageInfo"] = exp.Message;
                    Messages.AddErrorFlashMessage(exp.Message);
                    return View();
                }
        }
        #endregion

        #region POST: /Districts/EditCms/5

        [HttpPost]
        public ActionResult EditCms(Int16 id, District fromdata)
        {
                try
                {
                    var currentItem = eDataConttext.Districts.Single(e => e.ID == id);
                    currentItem.CityID = fromdata.CityID;
                    currentItem.Name = fromdata.Name;
                    currentItem.ShortName = fromdata.ShortName;
                    eDataConttext.SubmitChanges();
                    return RedirectToAction("IndexCms");
                }
                catch (Exception exp)
                {
                    ViewData["MessageInfo"] = exp.Message;
                    Messages.AddErrorFlashMessage(exp.Message);
                    return View();
                }
        }
        #endregion
    }
}
